package com.tsy.server.manage.controller;

import com.tsy.server.component.security.SecurityUtils;
import com.tsy.server.manage.model.dto.CapacityDeviceDTO;
import com.tsy.server.manage.model.dto.LiveTypeDTO;
import com.tsy.server.manage.service.ILiveStreamService;
import com.dji.sdk.common.HttpResultResponse;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * @author sean.zhou
 * @version 0.1
 * @date 2021/11/19
 */

@RestController
@Slf4j
@RequestMapping("${url.manage.prefix}/live")
public class LiveStreamController {

    @Autowired
    private ILiveStreamService liveStreamService;

    @Autowired
    private ObjectMapper mapper;

    /**
     * 从数据库中获取当前用户工作区中所有无人机的实时性能数据。
     * @param request
     * @return  实时能力数据
     */

    @ApiOperation("获取设备相机参数")
    @GetMapping("/capacity")
    public HttpResultResponse<List<CapacityDeviceDTO>> getLiveCapacity(HttpServletRequest request) {
        // 获取当前用户的信息。
        // todo 获取workSpaceId
        String workSpaceId = SecurityUtils.getWorkspaceId();
        List<CapacityDeviceDTO> liveCapacity = liveStreamService.getLiveCapacity(workSpaceId);

        return HttpResultResponse.success(liveCapacity);
    }

    /**
     * 根据Web端传入的参数发起直播。
     * @param liveParam 直播参数
     * @return
     */
    @ApiOperation("开启直播")
    @PostMapping("/streams/start")
    public HttpResultResponse liveStart(@RequestBody LiveTypeDTO liveParam) {
        return liveStreamService.liveStart(liveParam);
    }

    /**
     * 根据Web端传入的参数停止直播。
     * @param liveParam 直播参数
     * @return
     */
    @ApiOperation("停止直播")
    @PostMapping("/streams/stop")
    public HttpResultResponse liveStop(@RequestBody LiveTypeDTO liveParam) {
        return liveStreamService.liveStop(liveParam.getVideoId());
    }

    /**
     * 根据Web端传入的参数设置直播质量。
     * @param liveParam 直播参数
     * @return
     */
    @ApiOperation("设置直播质量")
    @PostMapping("/streams/update")
    public HttpResultResponse liveSetQuality(@RequestBody LiveTypeDTO liveParam) {
        return liveStreamService.liveSetQuality(liveParam);
    }

    @ApiOperation("镜头切换")
    @PostMapping("/streams/switch")
    public HttpResultResponse liveLensChange(@RequestBody LiveTypeDTO liveParam) {
        return liveStreamService.liveLensChange(liveParam);
    }

}